package timing;

import java.awt.Desktop;
import java.awt.EventQueue;
import java.awt.Rectangle;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;


public class Program {
	
	private static String tempDir = System.getProperty ("java.io.tmpdir"); 
	final static String ekstensjaPath = getTempDir() + "Timing4.07.bin"; 
	final static String pdf = getTempDir() + "Timing4.07-statystyki";
	
	static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	static DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
	static DateFormat timeFormatasfilename = new SimpleDateFormat("HH-mm-ss");

	public static void main(String[] args) throws FileNotFoundException, IOException, ParseException, ClassNotFoundException, DocumentException {
		try {
			//wczytajWszystkoDo();
		} catch (Exception e1) {
		}
		Zadanie.dodajRecznieZadanie("2014/08/25", 1, "Abak", "10:00:00", "12:00:00"); // test
		Zadanie.dodajRecznieZadanie("2014/08/25", 2, "Abak", "13:30:00", "15:00:00"); // test
		Zadanie.dodajRecznieZadanie("2014/08/25", 3, "Test", "15:30:00", "16:00:00"); // test
		Zadanie.dodajRecznieZadanie("2014/08/25", 4, "Inny", "17:30:00", "18:00:00"); // test
		
		Zadanie.dodajRecznieZadanie("2014/07/10", 1, "Abak", "13:30:00", "15:00:00"); // test
		Zadanie.dodajRecznieZadanie("2014/07/10", 2, "Abak", "13:30:00", "15:00:00"); // test

		
		inicjalizujAll();
		//zapiszWszystkoDo();
		
	}

	
	static void zapiszWszystkoDo() throws FileNotFoundException, IOException {
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(ekstensjaPath));
		Zadanie.zapiszEkstensje(out);
		out.close();
	}
	
	static void wczytajWszystkoDo() throws FileNotFoundException, IOException, ClassNotFoundException {
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(ekstensjaPath));
		Zadanie.wczytajEkstensje(in);
		in.close();
	}
	
	private static void inicjalizujAll() {
		EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MyFrameStart();
            }
        });
	}
	
	public static String instrukcja() {
		String info = "";
		info += "Kliknij START, by rozpocząć nowe zadanie" + "\n";
		info += "Wpisz opis zadania i kliknij ENTER." + "\n";
		info += "Kliknij STOP, by zamknąć zadanie." + "\n";
		info += "Kliknij ZAKOŃCZ/ZACZNIJ, by zamknąć zadanie i od razu otworzyć nowe." + "\n";
		info += "Kliknij PAUZA, aby przerwać bieżące zadanie, następnie PLAY, aby je wznowić." + "\n";

		return info;
	}
	
	public static void otworzPDF() {
		try {
			 
			File pdfFile = new File(Program.pdf+"_" + dateFormat.format(new Date())+"_"+timeFormatasfilename.format(new Date())+".pdf");
			if (pdfFile.exists()) {
	 
				if (Desktop.isDesktopSupported()) {
					Desktop.getDesktop().open(pdfFile);
				} else {
					System.out.println("Awt Desktop is not supported!");
				}
	 
			} else {
				System.out.println("File is not exists!");
			}
	 
			System.out.println("Done");
	 
		  } catch (Exception ex) {
			ex.printStackTrace();
		  }
	}
	
	public static void generujPDF() throws FileNotFoundException, DocumentException  {
		Rectangle pagesize = new Rectangle();
		com.itextpdf.text.Document document = new com.itextpdf.text.Document(PageSize.A4, 50, 50, 50, 50);
		document.addCreationDate();
		PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(Program.pdf+"_" + dateFormat.format(new Date())+"_"+timeFormatasfilename.format(new Date())+".pdf"));
		
		
		document.open();
		Paragraph a = new Paragraph(wyczyscZPolskichZnakow("STATYSTYKI wygenerowane " + dateFormat.format(new Date())+", "+timeFormatasfilename.format(new Date())), new Font(FontFamily.COURIER, 18));
		document.add(a);
		Paragraph b = new Paragraph(wyczyscZPolskichZnakow(Zadanie.wyswietlZadaniaGrupowanePoDacie()), new Font(FontFamily.COURIER, 10));
		document.add(b);
		document.close();
	}
	
	private static String wyczyscZPolskichZnakow(String all) {
		String all_n = all.replaceAll("ń", "n");
		String all_o = all_n.replaceAll("ó", "o");
		String all_e = all_o.replaceAll("ę", "e");
		String all_a = all_e.replaceAll("ą", "a");
		String all_z1 = all_a.replaceAll("ż", "z");
		String all_z2 = all_z1.replaceAll("ź", "z");
		String all_s = all_z2.replaceAll("ś", "s");
		String all_fin = all_s.replaceAll("ł", "l");
		return all_fin;	
	}


	public static String getTempDir() {
		return tempDir;
	}


	public static void setTempDir(String tempDir) {
		Program.tempDir = tempDir;
	}
}